<template>
  <div class="questions">
    <!-- 顶部选择区 -->
    <el-card class="top">
      <el-form
        ref="form"
        :model="form"
        label-width="50px"
        size="normal"
        class="form"
      >
        <!-- 第一栏 -->
        <el-row>
          <el-col :span="6">
            <el-form-item prop="subject" label="学科">
              <el-select v-model="form.subject" placeholder="请选择">
                <el-option
                  v-for="item in subjectList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="阶段" prop="step">
              <el-select v-model="form.step" placeholder="请选择">
                <el-option
                  v-for="item in stepList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业" prop="enterprise">
              <el-select v-model="form.enterprise" placeholder="请选择">
                <el-option
                  v-for="item in enterpriseList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="题型" prop="type">
              <el-select v-model="form.type" placeholder="请选择">
                <el-option
                  v-for="item in typeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 第二栏 -->
        <el-row>
          <el-col :span="6">
            <el-form-item label="难度" prop="difficulty">
              <el-select v-model="form.difficulty" placeholder="请选择">
                <el-option
                  v-for="item in difficultList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="作者" prop="username">
              <el-input v-model="form.username" style="width: 222px" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择">
                <el-option
                  v-for="item in statusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="日期" prop="create_date">
              <el-date-picker
                v-model="form.create_date"
                type="date"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 第三栏 -->
        <el-row>
          <el-col :span="12">
            <el-form-item prop="title" label="标题">
              <el-input v-model="form.title" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-button type="primary" @click="search">搜索</el-button>
            <el-button type="plain" @click="clean">清除</el-button>
            <el-button type="primary" @click="add">+新增试题</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <!-- 底部表格栏 -->
    <el-card class="bottom">
      <el-table :data="tableData">
        <el-table-column label="序号" width="60px">
          <template v-slot="{ $index }">
            {{ (form.page - 1) * form.limit + $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="题目" width="200px">
          <template v-slot="{ row }">
            <span v-html="showTitle(row)"></span>
          </template>
        </el-table-column>
        <el-table-column
          label="学科.阶段"
          :formatter="formatSubject"
        ></el-table-column>
        <el-table-column label="题型" :formatter="formatType"></el-table-column>
        <el-table-column prop="enterprise_name" label="企业"></el-table-column>
        <el-table-column prop="username" label="创建者"></el-table-column>
        <el-table-column label="状态">
          <template v-slot="{ row }">
            <el-switch
              v-model="row.status"
              active-color="#409eff"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              @change="close(row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="reads" label="访问量"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template v-slot="{ row }">
            <div class="btn">
              <el-button type="text" @click="edit(row)">编辑</el-button>
              <el-button type="text" @click="changeStatus(row)">
                {{ row.status === 0 ? '启用' : '禁用' }}
              </el-button>
              <el-button type="text" @click="del(row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="form.page"
        :page-sizes="[1, 5, 10, 20, 50, 100, 200, 300, 400]"
        :page-size.sync="form.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="text-align: center; margin-top: 20px"
      >
      </el-pagination>
    </el-card>
    <!-- 新增题目 -->
    <Add
      ref="addWindow"
      :subjectList="subjectList"
      :stepList="stepList"
      :enterpriseList="enterpriseList"
      :typeList="typeList"
      :difficultList="difficultList"
      @getQuestionList="getQuestionList"
    />
  </div>
</template>

<script>
import { subjectList } from '@/api/subjectList'
import { enterpriseList } from '@/api/business'
import {
  getQuestionListAPI,
  setQuestionStatusAPI,
  delQuestionAPI
} from '@/api/questions'
import jsCookie from 'js-cookie'
export default {
  components: {
    Add: () => import('./components/Add')
  },
  data () {
    return {
      // 学科列表
      subjectList: [],
      // 企业列表
      enterpriseList: [],
      // 阶段列表
      stepList: [
        { label: '初级', value: 1 },
        { label: '中级', value: 2 },
        { label: '高级', value: 3 }
      ],
      // 题型列表
      typeList: [
        { label: '单选', value: 1 },
        { label: '多选', value: 2 },
        { label: '简答', value: 3 }
      ],
      // 难度列表
      difficultList: [
        { label: '简单', value: 1 },
        { label: '一般', value: 2 },
        { label: '困难', value: 3 }
      ],
      // 状态列表
      statusList: [
        { label: '禁用', value: 0 },
        { label: '启用', value: 1 }
      ],
      // 表单数据
      form: {
        subject: '',
        step: '',
        enterprise: '',
        type: '',
        difficulty: '',
        username: '',
        status: '',
        create_date: '',
        title: '',
        // 分页
        page: 1,
        limit: +jsCookie.get('limit') || 10
      },
      // 表格数据
      tableData: [{}],
      total: 100
    }
  },
  created () {
    this.getQuestionList()
  },
  mounted () {
    this.getSubjectList()
    this.getEnterpriseList()
  },
  methods: {
    // 页容量改变时调用
    handleSizeChange () {
      // 页容量持久化
      jsCookie.set('limit', this.form.limit)
      this.getQuestionList()
    },
    // 页码改变时调用
    handleCurrentChange () {
      this.getQuestionList()
    },
    // 获取学科列表
    async getSubjectList () {
      const res = await subjectList()
      this.subjectList = res.data.items
    },
    // 获取企业列表
    async getEnterpriseList () {
      const res = await enterpriseList()
      this.enterpriseList = res.data.items
    },
    // 获取题目列表
    async getQuestionList () {
      const res = await getQuestionListAPI(this.form)
      // console.log(res)
      this.tableData = res.data.items
      this.total = res.data.pagination.total
    },
    // 格式化学科阶段
    formatSubject (row, column, cellValue, index) {
      // console.log(row)
      if (row.step === 1) {
        return row.subject_name + '.初级'
      } else if (row.step === 2) {
        return row.subject_name + '.中级'
      } else if (row.step === 3) {
        return row.subject_name + '.高级'
      } else {
        return '-'
      }
    },
    // 格式化题型
    formatType (row, column, cellValue, index) {
      if (row.type === 1) {
        return '单选'
      } else if (row.type === 2) {
        return '多选'
      } else if (row.type === 3) {
        return '简答'
      } else {
        return '-'
      }
    },
    // 显示题目
    showTitle (row) {
      return row.title
    },
    // 改变题目状态
    async changeStatus (row) {
      await setQuestionStatusAPI({
        id: row.id
      })
      this.$message.success('改变题目状态成功')
      this.getQuestionList()
    },
    // 按钮状态转换
    async close (row) {
      await setQuestionStatusAPI({
        id: row.id
      })
      this.$message.success('改变题目状态成功')
      this.getQuestionList()
    },
    // 搜索
    search () {
      this.getQuestionList()
      if (this.tableData.length === 0) {
        this.$message.error('找不到该题库')
      }
    },
    // 清除
    clean () {
      this.$refs.form.resetFields()
      this.getQuestionList()
    },
    // 新增题库
    add () {
      this.$refs.addWindow.show = true
      this.$refs.addWindow.mode = 'add'
    },
    // 编辑题库
    edit (row) {
      this.$refs.addWindow.show = true
      this.$refs.addWindow.mode = 'edit'
      this.$refs.addWindow.form = JSON.parse(JSON.stringify(row))
      // 城市和多选都需要数组 ，但是row返回的是字符串
      // 使用split字符串转数组
      this.$refs.addWindow.form.city = row.city.split(',')
      this.$refs.addWindow.form.multiple_select_answer = row.multiple_select_answer.split(
        ','
      )
    },
    // 删除题库
    del (row) {
      this.$confirm('你是否要删除该题库？', '提示', {
        confirmButtonText: '确定删除',
        cancelButtonText: '点错啦',
        type: 'warning'
      })
        .then(async () => {
          // console.log(row)
          await delQuestionAPI({
            id: row.id
          })
          this.$message.success('删除成功')
          if (this.form.page > 1 && this.tableData.length === 1) {
            this.form.page--
          }
          this.getQuestionList()
        })
        .catch(() => {
          this.$message.error('删除失败')
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.questions {
  padding: 10px;
  .top {
    .form {
      font-weight: normal;
    }
  }
}
</style>
